<?php

namespace App\Kuafu\Product\Resources;

use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Product\Enum\CodeEnum;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\MissingValue;

class ProductResource extends JsonResource
{

    /**
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        if (!$this->whenLoaded("productStatusRel") instanceof MissingValue && $this->productStatusRel) {
            $price  = $this->productStatusRel->price > 0 ? $this->productStatusRel->price : $this->price;
            $status = $this->productStatusRel->status;
            list($showPos, $showApplet) = self::getClientShow($this->productStatusRel->showPlatform);
        } else {
            list($showPos, $showApplet) = self::getClientShow($this->showPlatform);
            $status = $this->status;
            $price  = $this->price;
        }

        return [
            "id"           => $this->id,
            "menuId"       => $this->menuId,
            "brandId"      => $this->brandId,
            "categoryId"   => $this->categoryId,
            "kind"         => $this->kind,
            "kindText"     => CodeEnum::KIND_MAP[$this->kind] ?? "-",
            "type"         => $this->type,
            "typeText"     => CodeEnum::TYPE_PRODUCT_TYPE_TEXT[$this->type] ?? "-",
            "name"         => $this->name,
            "desc"         => $this->desc,
            "imageUrl"     => picturePath($this->imageUrl),
            "unit"         => $this->unit,
            "price"        => $price,
            "costPrice"    => $this->costPrice,
            "vipPrice"     => $this->vipPrice,
            "sort"         => $this->sort,
            "isHot"        => $this->isHot,
            "isRecommend"  => $this->isRecommend,
            "showPlatform" => $this->showPlatform,
            "status"       => $status,
            "showPos"      => $showPos,
            "showApplet"   => $showApplet,
            "productList"  => $this->whenLoaded("productPackageRel") instanceof MissingValue ? null : PackageProductResource::collection($this->productPackageRel),
            "categoryName" => $this->whenLoaded("categoryRel") instanceof MissingValue ? "" : $this->categoryRel->name,
            "isMust"       => $this->productStatusRel->isMust ?? 0
        ]; // TODO: Change the autogenerated stub
    }


    /**
     * @param int $showPlatform
     * @return array
     */
    public static function getClientShow(int $showPlatform): array
    {
        $showArr = array_keys(CodeEnum::CLIENT_MAP);
        rsort($showArr);
        $showPos     = $showApplet = false;
        $decodeRules = CommonService::decodeRules($showPlatform, $showArr);
        if (in_array(CodeEnum::PRODUCT_SHOW_POS, $decodeRules)) {
            $showPos = true;
        }
        if (in_array(CodeEnum::PRODUCT_SHOW_APPLET, $decodeRules)) {
            $showApplet = true;
        }

        return [$showPos, $showApplet];
    }

}
